package com.yanqu.road.server.manager.activity.suitang.manager;

import com.yanqu.road.entity.activity.suitang.config.SuiTangBuffConfig;
import com.yanqu.road.entity.activity.suitang.config.SuiTangCityConfig;
import com.yanqu.road.entity.activity.suitang.config.SuiTangCityTypeConfig;
import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionJoinData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUserData;
import com.yanqu.road.entity.activity.suitang.entity.battle.CrossSuiTangBattlefield;
import com.yanqu.road.entity.activity.suitang.entity.battle.CrossSuiTangBattlefieldTeam;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangCity;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangCityEvent;
import com.yanqu.road.entity.activity.suitang.entity.group.CrossSuiTangGroup;
import com.yanqu.road.entity.activity.suitang.entity.rank.CrossSuiTangRankListModel;
import com.yanqu.road.entity.activity.suitang.entity.team.CrossSuiTangTeam;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionBuff;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionJoinMember;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionRound;
import com.yanqu.road.entity.activity.suitang.entity.user.CrossSuiTangUserRound;
import com.yanqu.road.entity.activity.suitang.enums.*;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingUserData;
import com.yanqu.road.entity.enums.eSceneType;
import com.yanqu.road.entity.enums.eTongBingRedDotType;
import com.yanqu.road.entity.scene.UserScene;
import com.yanqu.road.entity.scene.UserSceneGroup;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.server.manager.activity.suitang.activity.CrossSuiTangActivity;
import com.yanqu.road.server.manager.scene.CrossSceneMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBaseActivityMgr {
    // 对应活动
    public CrossSuiTangActivity activity = null;
    // 城市锁
    private static final Map<Integer, Map<Integer, Map<eSuiTangWarType, Map<Integer, Object>>>> cityLockMap = new ConcurrentHashMap<>();
    // 玩家锁
    private static final Map<Long, Object> playerLockMap = new ConcurrentHashMap<>();

    protected static RandomHelper randomHelper = new RandomHelper();


    public void init() {

    }

    public void setActivity(CrossSuiTangActivity activity) {
        this.activity = activity;
    }

    public int calcRound() {
        if (activity == null) {
            return 0;
        }
        return activity.calcRound();
    }

    public boolean isLockTime() {
        if (activity == null) {
            return false;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return false;
        }
        List<Long> applyTimeList = config.getParamConfig().getSuiTangCreateTeamTime();
        if (applyTimeList.size() < 3) {
            return false;
        }
        long now = System.currentTimeMillis();
        long lockTime = config.getActivityInfo().getBeginShowTime() * DateHelper.SECOND_MILLIONS +
                (applyTimeList.get(0) - 1) * DateHelper.DAY_MILLIONS + applyTimeList.get(2) * DateHelper.HOUR_MILLIONS;
        return now >= lockTime;
    }

    /**
     * 获取buff效果
     *
     * @return 看配置下标是哪个就拿哪个
     */
    public int getBuffAddition(String unionId, eSuiTangBuffType buffType) {
        int ret = 0;
        // 判断阵营
        if (unionId.isEmpty() || activity == null) {
            return ret;
        }
        CrossSuiTangData crossSuiTangData = activity.getCrossSuiTangData();
        if (crossSuiTangData == null) {
            return ret;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return ret;
        }

        // 从商会buff中找
        List<CrossSuiTangUnionBuff> list = getUnionData(unionId).getUnionBuffMap();
        if (list != null) {
            for (CrossSuiTangUnionBuff unionBuff : new ArrayList<>(list)) {
                SuiTangBuffConfig buffTypeConfig = config.getBuffConfigMap().get(unionBuff.getBuffId());
                if (buffTypeConfig != null) {
                    if (buffTypeConfig.getType() == buffType.getValue()) {
                        //buff开始时间大于本轮次开始时间，才加上
                        if (unionBuff.getBeginTime() >= activity.getConfig().getRoundBeginTime(calcRound())) {
                            // buff加到结果中
                            ret += StringUtils.parseInt(buffTypeConfig.getBuffEffect());
                        }
                    }
                }
            }
        }
        return ret;
    }

    /**
     * 获取buff效果
     *
     * @return 看配置下标是哪个就拿哪个
     */
    public List<Integer> getBattleBuffAddition(String unionId, eSuiTangBuffType buffType) {
        List<Integer> ret = new ArrayList<>();
        if (buffType != eSuiTangBuffType.Type3_DefCityBattleSpeedUp && buffType != eSuiTangBuffType.Type4_AttCityBattleSpeedDown) {
            return ret;
        }
        // 判断阵营
        if (unionId.isEmpty() || activity == null) {
            return ret;
        }
        CrossSuiTangData crossSuiTangData = activity.getCrossSuiTangData();
        if (crossSuiTangData == null) {
            return ret;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return ret;
        }

        // 从商会buff中找
        List<CrossSuiTangUnionBuff> list = getUnionData(unionId).getUnionBuffMap();
        if (list != null) {
            for (CrossSuiTangUnionBuff unionBuff : new ArrayList<>(list)) {
                SuiTangBuffConfig buffTypeConfig = config.getBuffConfigMap().get(unionBuff.getBuffId());
                if (buffTypeConfig != null) {
                    if (buffTypeConfig.getType() == buffType.getValue()) {
                        // buff加到结果中
                        ret.addAll(StringUtils.stringToIntegerList(buffTypeConfig.getBuffEffect(), "\\|"));
                    }
                }
            }
        }
        return ret;
    }

    /**
     * 获取buff效果
     *
     * @return 看配置下标是哪个就拿哪个
     */
    public int getBuffEffect(int buffId) {
        int ret = 0;
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return ret;
        }

        SuiTangBuffConfig buffTypeConfig = config.getBuffConfigMap().get(buffId);
        if (buffTypeConfig != null) {
            ret = StringUtils.parseInt(buffTypeConfig.getBuffEffect());
        }
        return ret;
    }

    public List<Integer> getBuff(long userId) {
        List<Integer> ret = new ArrayList<>();
        CrossSuiTangUserData userData = activity.getUserData(userId);
        if (userData == null) {
            return ret;
        }
        ret.addAll(getBuff(userData.getUnionUid(), eSuiTangBuffType.Type1_TeamSpeedUp));
        ret.addAll(getBuff(userData.getUnionUid(), eSuiTangBuffType.Type2_CityMuBingLing));
        ret.addAll(getBuff(userData.getUnionUid(), eSuiTangBuffType.Type3_DefCityBattleSpeedUp));
        ret.addAll(getBuff(userData.getUnionUid(), eSuiTangBuffType.Type4_AttCityBattleSpeedDown));
        ret.addAll(getBuff(userData.getUnionUid(), eSuiTangBuffType.Type7_ReadySoldierMaxNum));
        ret.addAll(getBuff(userData.getUnionUid(), eSuiTangBuffType.Type8_CityFoodProduce));
        return ret;
    }

    /**
     * 获取buff
     */
    public List<Integer> getBuff(String unionId, eSuiTangBuffType buffType) {
        List<Integer> ret = new ArrayList<>();
        // 判断阵营
        if (unionId.isEmpty() || activity == null) {
            return ret;
        }
        CrossSuiTangData crossSuiTangData = activity.getCrossSuiTangData();
        if (crossSuiTangData == null) {
            return ret;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return ret;
        }

        // 这几种buff从城池事件那边找
        List<CrossSuiTangUnionBuff> list = getUnionData(unionId).getUnionBuffMap();
        if (list != null) {
            for (CrossSuiTangUnionBuff unionBuff : new ArrayList<>(list)) {
                //buff开始时间大于本轮次开始时间，才加上
                if (unionBuff.getBeginTime() >= activity.getConfig().getRoundBeginTime(calcRound())) {
                    SuiTangBuffConfig buffTypeConfig = config.getBuffConfigMap().get(unionBuff.getBuffId());
                    if (buffTypeConfig != null) {
                        if (buffTypeConfig.getType() == buffType.getValue()) {
                            // buff加到结果中
                            ret.add(buffTypeConfig.getId());
                        }
                    }
                }
            }
        }
        return ret;
    }

    /**
     * 获取buff
     */
    public List<CrossSuiTangUnionBuff> getUnionBuff(String unionId, eSuiTangBuffType buffType) {
        List<CrossSuiTangUnionBuff> ret = new ArrayList<>();
        // 判断阵营
        if (unionId.isEmpty() || activity == null) {
            return ret;
        }
        CrossSuiTangData crossSuiTangData = activity.getCrossSuiTangData();
        if (crossSuiTangData == null) {
            return ret;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return ret;
        }

        // 这几种buff从城池事件那边找
        List<CrossSuiTangUnionBuff> list = getUnionData(unionId).getUnionBuffMap();
        if (list != null) {
            for (CrossSuiTangUnionBuff unionBuff : new ArrayList<>(list)) {
                //buff开始时间大于本轮次开始时间，才加上
                if (unionBuff.getBeginTime() >= activity.getConfig().getRoundBeginTime(calcRound())) {
                    SuiTangBuffConfig buffTypeConfig = config.getBuffConfigMap().get(unionBuff.getBuffId());
                    if (buffTypeConfig != null) {
                        if (buffTypeConfig.getType() == buffType.getValue()) {
                            // buff加到结果中
                            ret.add(unionBuff);
                        }
                    }
                }
            }
        }
        return ret;
    }

    public CrossSuiTangCity getUserCity(long userId, int cityId) {
        Map<Integer, CrossSuiTangCity> cityMap = getCurCityMap(userId);
        if (cityMap == null || cityMap.isEmpty()) {
            return null;
        }
        return cityMap.get(cityId);
    }

    public CrossSuiTangCity getCity(eSuiTangWarType warType, int actGroupId, int cityId) {
        Map<Integer, CrossSuiTangCity> cityMap = getCityMap(warType, actGroupId);
        if (cityMap == null || cityMap.isEmpty()) {
            return null;
        }
        return cityMap.get(cityId);
    }

    public CrossSuiTangCity getCity(long userId) {
        if (activity == null || userId <= 0) {
            return null;
        }
        Map<Integer, CrossSuiTangCity> curCityMap = getCurCityMap(userId);
        if (curCityMap == null || curCityMap.isEmpty()) {
            return null;
        }
        CrossSuiTangUserData userData = activity.getUserData(userId);
        CrossSuiTangTeam team = userData.getTeam();
        if (team == null) {
            return null;
        }
        return curCityMap.get(team.getCityId());
    }

    public CrossSuiTangCity getCurCity(long userId, int cityId) {
        Map<Integer, CrossSuiTangCity> curCityMap = getCurCityMap(userId);
        if (curCityMap == null || curCityMap.isEmpty()) {
            return null;
        }
        return curCityMap.get(cityId);
    }

    public Map<Integer, CrossSuiTangCity> getCurSceneCityMap(long userId) {
        UserScene userScene = CrossSceneMgr.getUserScene(userId);
        if (userScene == null) {
            return null;
        }

        List<Integer> list = activity.sceneKeyToList(userScene.getSceneId());
        if (list == null || list.isEmpty() || list.size() < 4) {
            return null;
        }
        return getCityMap(eSuiTangWarType.forValue(list.get(2)), list.get(3));
    }

    public Map<Integer, CrossSuiTangCity> getCurCityMap(long userId) {

        CrossSuiTangUserRound curUserRound = getCurUserRound(userId);
        if (curUserRound == null) {
            return null;
        }
        return getCityMap(curUserRound.getWarType(), curUserRound.getActGroupId());
    }

    public boolean isOut(long userId) {
        return getUnionRoundData(activity.getUserData(userId).getUnionUid()).isOut();
    }

    public Map<Integer, CrossSuiTangCity> getCityMap(eSuiTangWarType warType, int actGroupId) {
        if (activity == null) {
            return null;
        }
        CrossSuiTangData crossSuiTangData = activity.getCrossSuiTangData();
        if (crossSuiTangData == null) {
            return null;
        }
        Map<Integer, Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCity>>>> cityMap = crossSuiTangData.getCityMap();
        if (cityMap == null || cityMap.isEmpty()) {
            return null;
        }
        Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCity>>> warTypeMap = cityMap.get(calcRound());
        if (warTypeMap == null || warTypeMap.isEmpty()) {
            return null;
        }

        Map<Integer, Map<Integer, CrossSuiTangCity>> groupMap = warTypeMap.get(warType);
        if (groupMap == null || groupMap.isEmpty()) {
            return null;
        }

        return groupMap.get(actGroupId);
    }

    public Map<Integer, CrossSuiTangCityEvent> getCurCityEventMap(long userId) {
        CrossSuiTangUserRound curUserRound = getCurUserRound(userId);
        if (curUserRound == null) {
            return null;
        }
        return getCityEventMap(curUserRound.getWarType(), curUserRound.getActGroupId());
    }

    public Map<Integer, CrossSuiTangCityEvent> getCityEventMap(eSuiTangWarType warType, int actGroupId) {
        if (activity == null) {
            return null;
        }
        CrossSuiTangData crossSuiTangData = activity.getCrossSuiTangData();
        if (crossSuiTangData == null) {
            return null;
        }
        Map<Integer, Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCityEvent>>>> cityMap = crossSuiTangData.getCityEventMap();
        if (cityMap == null || cityMap.isEmpty()) {
            return null;
        }
        Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCityEvent>>> warTypeMap = cityMap.get(calcRound());
        if (warTypeMap == null || warTypeMap.isEmpty()) {
            return null;
        }

        Map<Integer, Map<Integer, CrossSuiTangCityEvent>> groupMap = warTypeMap.get(warType);
        if (groupMap == null || groupMap.isEmpty()) {
            return null;
        }

        return groupMap.get(actGroupId);
    }

    public Map<Integer, Map<Integer, CrossSuiTangCityEvent>> getCityEventGroupMap(eSuiTangWarType warType) {
        if (activity == null) {
            return null;
        }
        CrossSuiTangData crossSuiTangData = activity.getCrossSuiTangData();
        if (crossSuiTangData == null) {
            return null;
        }
        Map<Integer, Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCityEvent>>>> cityMap = crossSuiTangData.getCityEventMap();
        if (cityMap == null || cityMap.isEmpty()) {
            return null;
        }
        Map<eSuiTangWarType, Map<Integer, Map<Integer, CrossSuiTangCityEvent>>> warTypeMap = cityMap.get(calcRound());
        if (warTypeMap == null || warTypeMap.isEmpty()) {
            return null;
        }

        return warTypeMap.get(warType);
    }

    public CrossSuiTangUserRound getCurUserRound(long userId) {
        if (activity == null) {
            return null;
        }
        CrossSuiTangUserData userData = activity.getUserData(userId);
        if (userData == null || userData.getUserRoundMap() == null || userData.getUserRoundMap().isEmpty()) {
            return null;
        }
        CrossSuiTangUserRound userRound = userData.getUserRoundMap().get(calcRound());
        if (userRound == null) {
            return null;
        }
        return userRound;
    }

    public CrossSuiTangUserRound getCurUserRound(CrossSuiTangUserData userData) {
        if (activity == null) {
            return null;
        }
        if (userData == null || userData.getUserRoundMap() == null || userData.getUserRoundMap().isEmpty()) {
            return null;
        }
        CrossSuiTangUserRound userRound = userData.getUserRoundMap().get(calcRound());
        if (userRound == null) {
            return null;
        }
        return userRound;
    }

    public CrossSuiTangUnionJoinData getUnionJoin(String unionId) {
        if (activity == null) {
            return null;
        }
        Map<String, CrossSuiTangUnionJoinData> unionJoinDataMap = activity.getUnionJoinDataMap();
        if (unionJoinDataMap == null || unionJoinDataMap.isEmpty()) {
            return null;
        }
        CrossSuiTangUnionJoinData unionJoinData = unionJoinDataMap.get(unionId);
        if (unionJoinData == null) {
            return null;
        }
        return unionJoinDataMap.get(unionId);
    }

    public CrossSuiTangUnionJoinMember getUnionJoinMember(String unionId, long userId) {
        if (activity == null) {
            return null;
        }
        Map<String, CrossSuiTangUnionJoinData> unionJoinDataMap = activity.getUnionJoinDataMap();
        if (unionJoinDataMap == null || unionJoinDataMap.isEmpty()) {
            return null;
        }
        CrossSuiTangUnionJoinData unionJoinData = unionJoinDataMap.get(unionId);
        if (unionJoinData == null) {
            return null;
        }
        Map<Long, CrossSuiTangUnionJoinMember> memberMap = unionJoinData.getMemberMap();
        if (memberMap.isEmpty()) {
            return null;
        }
        return memberMap.get(userId);
    }

    public CrossSuiTangUnionData getUnionData(String unionId) {
        if (activity == null) {
            return null;
        }
        Map<String, CrossSuiTangUnionData> unionDataMap = activity.getUnionDataMap();
        if (unionDataMap == null || unionDataMap.isEmpty()) {
            return null;
        }
        return unionDataMap.get(unionId);
    }

    public CrossSuiTangUnionRound getUnionRoundData(String unionId) {
        do {
            if (activity == null) {
                break;
            }
            Map<String, CrossSuiTangUnionData> unionDataMap = activity.getUnionDataMap();
            if (unionDataMap == null || unionDataMap.isEmpty()) {
                break;
            }
            CrossSuiTangUnionData unionData = unionDataMap.get(unionId);
            if (unionData == null) {
                break;
            }
            Map<Integer, CrossSuiTangUnionRound> unionRoundMap = unionData.getUnionRoundMap();
            if (unionRoundMap == null || unionRoundMap.isEmpty()) {
                break;
            }
            return unionRoundMap.get(calcRound());
        } while (false);

        return null;
    }

    public CrossSuiTangUnionRound getUnionRoundData(String unionId, int round) {
        do {
            if (activity == null) {
                break;
            }
            Map<String, CrossSuiTangUnionData> unionDataMap = activity.getUnionDataMap();
            if (unionDataMap == null || unionDataMap.isEmpty()) {
                break;
            }
            CrossSuiTangUnionData unionData = unionDataMap.get(unionId);
            if (unionData == null) {
                break;
            }
            Map<Integer, CrossSuiTangUnionRound> unionRoundMap = unionData.getUnionRoundMap();
            if (unionRoundMap == null || unionRoundMap.isEmpty()) {
                break;
            }
            return unionRoundMap.get(round);
        } while (false);

        return null;
    }

    public Map<Integer, CrossSuiTangUnionRound> getUnionRoundMap(String unionId) {
        do {
            if (activity == null) {
                break;
            }
            Map<String, CrossSuiTangUnionData> unionDataMap = activity.getUnionDataMap();
            if (unionDataMap == null || unionDataMap.isEmpty()) {
                break;
            }
            CrossSuiTangUnionData unionData = unionDataMap.get(unionId);
            if (unionData == null) {
                break;
            }
            Map<Integer, CrossSuiTangUnionRound> unionRoundMap = unionData.getUnionRoundMap();
            if (unionRoundMap == null || unionRoundMap.isEmpty()) {
                break;
            }
            return unionRoundMap;
        } while (false);

        return null;
    }

    public CrossSuiTangRankListModel getCityRank(CrossSuiTangCity city, String unionId) {
        if (activity == null) {
            return null;
        }
        Map<Integer, Map<Integer, Map<String, CrossSuiTangRankListModel>>> warUserRankMap = activity.getWarUserRankMap().get(city.getWarType());
        if (warUserRankMap == null) {
            warUserRankMap = new ConcurrentHashMap<>();
            activity.getWarUserRankMap().put(city.getWarType(), warUserRankMap);
        }
        Map<Integer, Map<String, CrossSuiTangRankListModel>> groupRankMap = warUserRankMap.get(city.getActGroupId());
        if (groupRankMap == null) {
            groupRankMap = new ConcurrentHashMap<>();
            warUserRankMap.put(city.getActGroupId(), groupRankMap);
        }
        Map<String, CrossSuiTangRankListModel> rankListModelMap = groupRankMap.get(city.getCityId());
        if (rankListModelMap == null) {
            rankListModelMap = new ConcurrentHashMap<>();
            groupRankMap.put(city.getCityId(), rankListModelMap);
        }
        CrossSuiTangRankListModel rankListModel = rankListModelMap.get(unionId);
        if (rankListModel == null) {
            rankListModel = new CrossSuiTangRankListModel();
            rankListModelMap.put(unionId, rankListModel);
        }
        return rankListModel;
    }

    public Map<String, CrossSuiTangRankListModel> getCityRankMap(CrossSuiTangCity city) {
        if (activity == null) {
            return null;
        }
        Map<Integer, Map<Integer, Map<String, CrossSuiTangRankListModel>>> warUserRankMap = activity.getWarUserRankMap().get(city.getWarType());
        if (warUserRankMap == null) {
            warUserRankMap = new ConcurrentHashMap<>();
            activity.getWarUserRankMap().put(city.getWarType(), warUserRankMap);
        }
        Map<Integer, Map<String, CrossSuiTangRankListModel>> groupRankMap = warUserRankMap.get(city.getActGroupId());
        if (groupRankMap == null) {
            groupRankMap = new ConcurrentHashMap<>();
            warUserRankMap.put(city.getActGroupId(), groupRankMap);
        }
        Map<String, CrossSuiTangRankListModel> rankListModelMap = groupRankMap.get(city.getCityId());
        if (rankListModelMap == null) {
            rankListModelMap = new ConcurrentHashMap<>();
            groupRankMap.put(city.getCityId(), rankListModelMap);
        }
        return rankListModelMap;
    }

//    public CrossSuiTangRankListModel getUserRank() {
//        if (activity == null) {
//            return null;
//        }
//        return activity.getUserRank();
//    }
//
//    public CrossSuiTangRankListModel getUnionRank() {
//        if (activity == null) {
//            return null;
//        }
//        return activity.getUnionRankMap();
//    }

    public eSuiTangTeamStatus getTeamStatus(CrossSuiTangTeam team) {
        if (activity == null) {
            return eSuiTangTeamStatus.Free;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return eSuiTangTeamStatus.Free;
        }
        // 是否在移动中
        if (team.getArriveTime() > System.currentTimeMillis()) {
            return eSuiTangTeamStatus.Move;
        }
        // 未分配阵营时为0
        if (team.getCityId() == 0) {
            return eSuiTangTeamStatus.Free;
        }

        // 是否在交战中
        Map<Integer, CrossSuiTangCity> curCityMap = getCurCityMap(team.getUserId());
        if (curCityMap == null || curCityMap.isEmpty()) {
            return eSuiTangTeamStatus.Free;
        }
        CrossSuiTangCity city = curCityMap.get(team.getCityId());
        if (city != null
                && city.getBattlefield() != null
        ) {
            Map<Long, CrossSuiTangBattlefieldTeam> attackingTeamMap = city.getBattlefield().getAttackingTeamMap();
            if (attackingTeamMap != null
                    && attackingTeamMap.get(team.getUserId()) != null) {
                return eSuiTangTeamStatus.Battle;
            }
        }

        return eSuiTangTeamStatus.Free;
    }

    public boolean isHomeCity(long userId, int cityId) {
        SuiTangCityTypeConfig cityTypeConfig = getCityTypeConfig(getCurCity(userId, cityId));
        if (cityTypeConfig != null) {
            return cityTypeConfig.getType() == eSuiTangCityType.CITY_TYPE_HOME.getType();
        }
        return false;
    }

    public boolean isHomeCity(CrossSuiTangCity city) {
        SuiTangCityTypeConfig cityTypeConfig = getCityTypeConfig(city);
        if (cityTypeConfig != null) {
            return cityTypeConfig.getType() == eSuiTangCityType.CITY_TYPE_HOME.getType();
        }
        return false;
    }

    public boolean isHomeCity(CrossSuiTangCity city, String unionId) {
        if (unionId.isEmpty()) {
            return false;
        }
        CrossSuiTangUnionRound unionRoundData = getUnionRoundData(unionId);
        if (unionRoundData == null) {
            return false;
        }
        SuiTangCityTypeConfig cityTypeConfig = getCityTypeConfig(city);
        if (cityTypeConfig != null) {
            return cityTypeConfig.getType() == eSuiTangCityType.CITY_TYPE_HOME.getType()
                    && city.getCityId() == unionRoundData.getHomeId()
                    && city.getWarType() == unionRoundData.getWarType()
                    && city.getActGroupId() == unionRoundData.getActGroupId();
        }
        return false;
    }

    public CrossSuiTangUnionData getUnionDataByCity(CrossSuiTangCity city) {
        if (activity == null) {
            return null;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return null;
        }
        Map<Integer, SuiTangCityConfig> cityConfigMap = config.getCityConfigMap();
        if (cityConfigMap == null || cityConfigMap.isEmpty() || cityConfigMap.get(city.getCityId()) == null) {
            return null;
        }
        SuiTangCityConfig cityConfig = cityConfigMap.get(city.getCityId());
        if (cityConfig.getType() != eSuiTangCityType.CITY_TYPE_HOME.getType()) {
            return null;
        }

        Map<eSuiTangWarType, Map<Integer, CrossSuiTangGroup>> actGroupMapTemp = activity.getCrossSuiTangData().getActGroupMap().get(calcRound());
        if (actGroupMapTemp == null || actGroupMapTemp.isEmpty()
                || actGroupMapTemp.get(city.getWarType()) == null || actGroupMapTemp.get(city.getWarType()).isEmpty()) {
            return null;
        }
        Map<Integer, CrossSuiTangGroup> groupMap = actGroupMapTemp.get(city.getWarType());
        if (groupMap == null || groupMap.isEmpty()) {
            return null;
        }
        CrossSuiTangGroup group = groupMap.get(city.getActGroupId());
        if (group == null) {
            return null;
        }
        for (Map.Entry<String, Integer> entry : group.getUnionMap().entrySet()) {
            if (entry.getValue() == city.getCityId()) {
                return getUnionData(entry.getKey());
            }
        }
        return null;
    }

    public CrossSuiTangTeam getTeam(long userId) {
        if (activity == null) {
            return null;
        }
        CrossSuiTangUserData userData = activity.getUserData(userId);
        if (userData == null) {
            return null;
        }
        return userData.getTeam();
    }

    public CrossSuiTangBattlefieldTeam getBattleTeam(CrossSuiTangCity city, long userId) {
        if (activity == null || city == null) {
            return null;
        }
        CrossSuiTangBattlefield battlefield = city.getBattlefield();
        if (battlefield == null) {
            return null;
        }
        CrossSuiTangBattlefieldTeam team = battlefield.getDefTeamMap().get(userId);
        if (team != null) {
            return team;
        }
        return battlefield.getAtkTeamMap().get(userId);
    }

    public int isLimitCityUserNum(long userId, int cityId) {
        CrossSuiTangTeam team = getTeam(userId);
        if (team == null) {
            return GameErrorCode.E_SUI_TANG_PARAM_ERR;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null || config.getCityConfigMap() == null || config.getCityConfigMap().isEmpty()) {
            return GameErrorCode.E_SUI_TANG_CONFIG_NOT_FOUND;
        }

        SuiTangCityConfig suiTangCityConfig = config.getCityConfigMap().get(cityId);
        if (suiTangCityConfig == null) {
            return GameErrorCode.E_SUI_TANG_CITY_NO_EXIST;
        }

        Map<Integer, CrossSuiTangCity> curCityMap = getCurCityMap(team.getUserId());
        if (curCityMap == null || curCityMap.isEmpty() || curCityMap.get(cityId) == null) {
            return GameErrorCode.E_SUI_TANG_CONFIG_NOT_FOUND;
        }
        CrossSuiTangCity toCity = curCityMap.get(cityId);
        SuiTangCityTypeConfig cityTypeConfig = config.getCityTypeConfigMap().get(config.getCityTypeConfigKey(toCity.getWarType(), suiTangCityConfig.getType()));
        if (cityTypeConfig == null) {
            return GameErrorCode.E_SUI_TANG_CONFIG_NOT_FOUND;
        }
        CrossSuiTangUserData userData = activity.getUserData(team.getUserId());
        boolean isDef = Objects.equals(toCity.getUnionId(), userData.getUnionUid());
        int defenseMax = cityTypeConfig.getDefenseMax();
        int attackMax = cityTypeConfig.getAttack();
        CrossSuiTangBattlefield battlefield = toCity.getBattlefield();
        if (battlefield == null) {
            return GameErrorCode.E_SUI_TANG_PARAM_ERR;
        }
        if (isDef) {
            if (battlefield.getDefTeamMap().size() - toCity.getNpcGuardList().size() >= defenseMax) {
                return GameErrorCode.E_SUI_TANG_CITY_DEF_MAX;
            }
        } else {
            if (battlefield.getAtkTeamMap().size() >= attackMax) {
                return GameErrorCode.E_SUI_TANG_CITY_ATTACK_MAX;
            }
        }

        return 0;
    }

    public int getBuffIdByType(eSuiTangBuffType buffType) {
        for (SuiTangBuffConfig suiTangBuffConfig : activity.getConfig().getBuffConfigMap().values()) {
            if (suiTangBuffConfig == null) {
                continue;
            }
            if (suiTangBuffConfig.getType() == buffType.getValue()) {
                return suiTangBuffConfig.getId();
            }
        }
        return 0;
    }
    public SuiTangCityTypeConfig getCityTypeConfig(CrossSuiTangCity city) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return null;
        }
        Map<Integer, SuiTangCityTypeConfig> cityTypeConfigMap = config.getCityTypeConfigMap();
        Map<Integer, SuiTangCityConfig> cityConfigMap = config.getCityConfigMap();
        if (cityConfigMap == null || cityConfigMap.isEmpty()
                || cityTypeConfigMap == null || cityTypeConfigMap.isEmpty()
        ) {
            return null;
        }
        SuiTangCityConfig cityConfig = cityConfigMap.get(city.getCityId());
        if (cityConfig == null) {
            return null;
        }

        SuiTangCityTypeConfig suiTangCityTypeConfig = cityTypeConfigMap.get(config.getCityTypeConfigKey(city.getWarType(), cityConfig.getType()));
        if (suiTangCityTypeConfig == null) {
            return null;
        }
        return suiTangCityTypeConfig;
    }

    public SuiTangCityConfig getCityConfig(CrossSuiTangCity city) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return null;
        }
        Map<Integer, SuiTangCityConfig> cityConfigMap = config.getCityConfigMap();
        if (cityConfigMap == null || cityConfigMap.isEmpty()) {
            return null;
        }
        SuiTangCityConfig cityConfig = cityConfigMap.get(city.getCityId());
        if (cityConfig == null) {
            return null;
        }
        return cityConfig;
    }

    /*
     * getUserWarType
     */
    public eSuiTangWarType getUserWarType(long userId) {
        CrossSuiTangUserRound userRound = getCurUserRound(userId);
        if (userRound != null) {
            return userRound.getWarType();
        }
        return eSuiTangWarType.WAR_TYPE_YIJUN;
    }

    /*
     * getSuiTangGroup 获取隋唐指定分组商会
     */
    public CrossSuiTangGroup getSuiTangGroup(eSuiTangWarType warType, int actGroupId) {
        Map<eSuiTangWarType, Map<Integer, CrossSuiTangGroup>> eSuiTangWarTypeMapMap = activity.getCrossSuiTangData().getActGroupMap().get(calcRound());
        if (eSuiTangWarTypeMapMap == null || eSuiTangWarTypeMapMap.isEmpty()) {
            return null;
        }
        Map<Integer, CrossSuiTangGroup> groupMap = eSuiTangWarTypeMapMap.get(warType);
        if (groupMap == null || groupMap.isEmpty()) {
            return null;
        }
        return groupMap.get(actGroupId);
    }

    /*
     * getUnionWarType
     */
    public eSuiTangWarType getUnionWarType(String unionId) {
        CrossSuiTangUnionRound unionRound = getUnionRoundData(unionId);
        if (unionRound != null) {
            return unionRound.getWarType();
        }
        return eSuiTangWarType.WAR_TYPE_YIJUN;
    }

    /**
     * 添加红点
     */
    public void addRedDot(CrossSuiTangUserData userData, eSuiTangRedDotType type) {
        if (userData.getRedDotMap().containsKey(type.getValue())) {
            return;
        }
        userData.getRedDotMap().put(type.getValue(), System.currentTimeMillis());
        userData.setUpdateOption();

        SuiTang.SuiTangSyncRedDotRespMsg.Builder builder = SuiTang.SuiTangSyncRedDotRespMsg.newBuilder();
        builder.addType(type.getValue());
        activity.sendPacket(ClientProtocol.U_SUI_TANG_SYNC_RED_DOT, builder, userData.getUserId());
    }

    public void addRedDotToAll(List<Long> userIds, eSuiTangRedDotType type) {
        for (Long userId : userIds) {
            CrossSuiTangUserData userData = activity.getUserData(userId);
            if (userData.getRedDotMap().containsKey(type.getValue())) {
                continue;
            }
            userData.getRedDotMap().put(type.getValue(), System.currentTimeMillis());
            userData.setUpdateOption();
        }

        SuiTang.SuiTangSyncRedDotRespMsg.Builder builder = SuiTang.SuiTangSyncRedDotRespMsg.newBuilder();
        builder.addType(type.getValue());
        activity.sendPacketToAll(ClientProtocol.U_SUI_TANG_SYNC_RED_DOT, builder, userIds);
    }

    public void readRedDotByCmd(long userId, int type) {
        if (activity.isAble(userId)) {
            CrossSuiTangUserData userData = activity.getUserData(userId);
            userData.getRedDotMap().remove(type);
            userData.setUpdateOption();
        }
    }

    public Object getPlayerLock(long userId) {
        if (!playerLockMap.containsKey(userId)) {
            synchronized (playerLockMap) {
                if (!playerLockMap.containsKey(userId)) {
                    playerLockMap.put(userId, new Object());
                }
            }
        }
        return playerLockMap.get(userId);
    }

//    public Lock getPlayerLock(long userId) {
//        if (!playerLockMap.containsKey(userId)) {
//            synchronized (playerLockMap) {
//                if (!playerLockMap.containsKey(userId)) {
//                    playerLockMap.put(userId, new ReentrantLock());
//                }
//            }
//        }
//        return playerLockMap.get(userId);
//    }

    /**
     * 将城市锁升为战场锁
     * @param city
     * @return
     */
    public Object getCityLock(CrossSuiTangCity city) {
        if (city == null || activity == null || activity.getConfig() == null) {
            return null;
        }
        eSuiTangWarType warType = city.getWarType();
        int actGroupId = city.getActGroupId();
        Map<Integer, Map<eSuiTangWarType, Map<Integer, Object>>> activityMap = cityLockMap.get(activity.getConfig().getActivityId());
        if (activityMap == null) {
            activityMap = new ConcurrentHashMap<>();
            cityLockMap.put(activity.getConfig().getActivityId(), activityMap);
        }
        Map<eSuiTangWarType, Map<Integer, Object>> eSuiTangWarTypeMap = activityMap.get(activity.getGroupId());
        if (eSuiTangWarTypeMap == null) {
            eSuiTangWarTypeMap = new ConcurrentHashMap<>();
            activityMap.put(activity.getGroupId(), eSuiTangWarTypeMap);
        }
        Map<Integer, Object> groupMap = eSuiTangWarTypeMap.get(warType);
        if (groupMap == null) {
            groupMap = new ConcurrentHashMap<>();
            eSuiTangWarTypeMap.put(warType, groupMap);
        }
        if (!groupMap.containsKey(actGroupId)) {
            synchronized (groupMap) {
                if (!groupMap.containsKey(actGroupId)) {
                    groupMap.put(actGroupId, new Object());
                }
            }
        }
        return groupMap.get(actGroupId);
    }
}
